package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Competition;
import cn.wjh.business.domain.Works;
import cn.wjh.business.domain.WorksMark;
import cn.wjh.business.mapper.WorksMarkMapper;
import cn.wjh.business.service.ICompetitionService;
import cn.wjh.business.service.IWorksMarkService;
import cn.wjh.business.service.IWorksService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作品打分Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-21
 */
@Service
public class WorksMarkServiceImpl extends ServiceImpl<WorksMarkMapper, WorksMark> implements IWorksMarkService {
	@Resource
	private WorksMarkMapper worksMarkMapper;
	@Resource
	private IWorksService worksService;
	@Resource
	private ICompetitionService competitionService;

	/**
	 * 查询作品打分
	 *
	 * @param markId 作品打分主键
	 * @return 作品打分
	 */
	@Override
	public WorksMark selectWorksMarkByMarkId(Long markId) {
		return worksMarkMapper.selectWorksMarkByMarkId(markId);
	}

	/**
	 * 查询作品打分列表
	 *
	 * @param worksMark 作品打分
	 * @return 作品打分
	 */
	@Override
	public List<WorksMark> selectWorksMarkList(WorksMark worksMark) {
		return worksMarkMapper.selectWorksMarkList(worksMark);
	}

	/**
	 * 新增作品打分
	 *
	 * @param worksMark 作品打分
	 * @return 结果
	 */
	@Override
	public int insertWorksMark(WorksMark worksMark) {
		Long userId = SecurityUtils.getUserId();//当前用户

		Long worksId = worksMark.getWorksId();//作品Id
		Works works = worksService.getById(worksId);//作品
		/*作品不存在*/
		if (StringUtils.isNull(works)) throw new ServiceException("当前作品不存在,无法点评!");

		Long competitionId = works.getCompetitionId();//比赛Id
		Competition competition = competitionService.getById(competitionId);//比赛
		/*比赛不存在*/
		if (StringUtils.isNull(competition)) throw new ServiceException("当前作品所在的比赛不存在,无法点评!");

		/*不是当前赛项的评委*/
		List<Long> judgeIds = competition.getJudgeIds();
		if (judgeIds.isEmpty() || !judgeIds.contains(userId))
			throw new ServiceException("您非本赛事评委,无法点评!");

		/*正常点评*/
		worksMark.setCreateTime(DateUtils.getNowDate());
		worksMark.setCreateBy(SecurityUtils.getUsername());//当前评委名字
		worksMark.setUserId(userId);//当前评委Id
		try {
			return worksMarkMapper.insertWorksMark(worksMark);
		} catch (DuplicateKeyException e) {
			throw new ServiceException("该作品已被点评!", 400);
		}
	}

	/**
	 * 修改作品打分
	 *
	 * @param worksMark 作品打分
	 * @return 结果
	 */
	@Override
	public int updateWorksMark(WorksMark worksMark) {
		return worksMarkMapper.updateWorksMark(worksMark);
	}

	/**
	 * 批量删除作品打分
	 *
	 * @param markIds 需要删除的作品打分主键
	 * @return 结果
	 */
	@Override
	public int deleteWorksMarkByMarkIds(Long[] markIds) {
		return worksMarkMapper.deleteWorksMarkByMarkIds(markIds);
	}

	/**
	 * 删除作品打分信息
	 *
	 * @param markId 作品打分主键
	 * @return 结果
	 */
	@Override
	public int deleteWorksMarkByMarkId(Long markId) {
		return worksMarkMapper.deleteWorksMarkByMarkId(markId);
	}

	/**
	 * 获取作品打分详细
	 *
	 * @param worksId 作品Id
	 * @param userId  用户Id
	 * @return 打分详细
	 */
	@Override
	public WorksMark getInfoByWorksIdAndUserId(Long worksId, Long userId) {
		LambdaQueryWrapper<WorksMark> selectWrapper = new QueryWrapper<WorksMark>().lambda()
				.eq(WorksMark::getWorksId, worksId)
				.eq(WorksMark::getUserId, userId);
		return this.getOne(selectWrapper);
	}
}
